export default {
    name: 'AddressManageView',
    data() {
        return {
            dialogVisible: false,
            addressList: [],
            labelPosition: 'right',
            formLabelAlign: {
                addressDdetails: '',
                addressPhone: '',
                addressReceive: '',
                userId: localStorage.getItem('currentUser').split('id')[1].split('"')[2]
            },
            tableData: []
        }
    },
    created() {
        this.getAddressAll()
    },
    methods: {
        // 删除地址
        delAddress(e) {
            console.log('e', e)
            this.$confirm('地址删除后不可恢复, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.defaults.headers.common['Authorization'] = localStorage['token']
                this.axios.delete('api/delAddress', { params: { id: e } })
                    .then(res => {
                        this.getAddressAll()
                        this.$message({
                            type: 'success',
                            duration: 1000,
                            message: '删除成功'
                        });
                    })
            }).catch(() => {
            });
        },
        indexMethod(index) {
            return index + 1;
        },
        // 查询地址
        getAddressAll() {
            this.axios.defaults.headers.common['Authorization'] = localStorage['token'];
            console.log('this.formLabelAlign.userId : ', this.formLabelAlign.userId)
            this.axios.get('api/getAddressInfo', { params: { uesrId: this.formLabelAlign.userId } })
                .then(res => {
                    this.tableData = res.data.data;
                    console.log('res.data.data: ', res.data.data)
                    // for (let i = 0; i < this.orders.length; i++) {
                    //     this.orders[i].addTime = this.timeTrans(this.orders[i].addTime, 2)
                    // }
                    // this.getOrders()
                })
        },
        // 添加地址
        addAdress() {
            console.log('this.formLabelAlign', this.formLabelAlign.userId)
            if (!this.formLabelAlign.addressDdetails || !this.formLabelAlign.addressPhone || !this.formLabelAlign.addressReceive) {
                this.$message({
                    type: 'error',
                    duration: 1000,
                    message: '请填写完整信息'
                });
                return
            }
            this.axios.defaults.headers.common['Authorization'] = localStorage['token'];
            this.axios.post('api/addAddress',
                { address: this.formLabelAlign })
                .then(res => {
                    this.$message({
                        type: 'success',
                        duration: 1000,
                        message: '添加成功'
                    });
                    this.getAddressAll()
                    this.dialogVisible = false
                    this.formLabelAlign = {
                        addressDdetails: '',
                        addressPhone: '',
                        addressReceive: '',
                        userId: localStorage.getItem('currentUser').split('id')[1].split('"')[2]
                    }
                })
        },
        handleClose(done) {
            done();
        }
    }
}